package com.puff.controller.user;

import com.github.pagehelper.Page;
import com.puff.dto.OrdersPaymentDTO;
import com.puff.dto.OrdersSubmitDTO;
import com.puff.dto.PaymentDTO;
import com.puff.dto.UserReviewDTO;
import com.puff.entity.OrderDetail;
import com.puff.entity.Orders;
import com.puff.result.PageResult;
import com.puff.result.Result;
import com.puff.service.OrderService;
import com.puff.vo.OrderPaymentVO;
import com.puff.vo.OrderSubmitVO;
import com.puff.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "C端-订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("submit用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单，参数为:{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        if (orderSubmitVO == null){
            return Result.error("超出配送范围");
        }
        return Result.success(orderSubmitVO);
    }

//    /**
//     * 订单支付
//     * @param ordersPaymentDTO
//     * @return
//     */
//    @PutMapping("/payment")
//    @ApiOperation("订单支付")
//    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
//        log.info("订单支付：{}", ordersPaymentDTO);
//        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
//        log.info("生成预支付交易单：{}", orderPaymentVO);
//        return Result.success(orderPaymentVO);
//    }

    /**
     * 订单支付
     * @param paymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result payment(@RequestBody PaymentDTO paymentDTO){
        log.info("订单支付：{}", paymentDTO);
        orderService.payment(paymentDTO);
        return Result.success();
    }

    /**
     * 查询历史订单
     * @param page
     * @param pageSize
     * @param status 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> getHistoryOrders(Integer page,Integer pageSize,Integer status){
        log.info("查询历史订单,page:{},pageSize:{},status:{}",page,pageSize,status);
        PageResult pageResult = orderService.pageQuery(page,pageSize,status);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id){
        log.info("查询订单详情,订单id:{}",id);
        OrderVO orderVO = orderService.details(id);
        return Result.success(orderVO);
    }

    /**
     * 用户取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("cancel用户取消订单")
    public Result cancel(@PathVariable Long id) throws Exception {
        log.info("用户取消订单,订单id:{}",id);
        orderService.userCancelById(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable Long id){
        log.info("再来一单，订单id：{}",id);
        orderService.repetition(id);
        return Result.success();
    }

    /**
     * 客户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public Result reminder(@PathVariable("id") Long id){
        log.info("客户催单:{}",id);
        orderService.reminder(id);
        return Result.success();
    }

    /**
     * 用户评价订单
     * @param userReviewDTO
     * @return
     */
    @PutMapping("/review")
    @ApiOperation("用户评价订单")
    public Result review(@RequestBody UserReviewDTO userReviewDTO){
        log.info("用户评价订单:{}",userReviewDTO);
        orderService.review(userReviewDTO);
        return Result.success();
    }
}
